package lib.bone.admin.controller.nepu;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.nepudata.domain.entity.*;
import lib.bone.nepudata.service.ResearchServiceCtl;
import lib.bone.system.domain.entity.SysUser;
import lib.bone.system.domain.vo.SysUserBasic;
import lib.bone.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 */
@Deprecated
@ApiIgnore
@Slf4j
//@RestController
//@RequestMapping("nepu")
@Api(tags = "参与者查询")
public class NepuParticipantController {
//    @Autowired
//    NepuResearchWorkloadService workloadService;

    @Autowired
    SysUserService userService;
    

   @Autowired
   ResearchServiceCtl researchServiceCtl;

//    @GetMapping("workload/{id}/part")
//    @ApiOperation(value = "查询参与人员(id)",notes = "")
//    public ResponseDTO getParticipantByWorkload(@PathVariable("id") String id){
//        Optional<NepuResearchWorkload> workloadOp = workloadService.getById(id);
//        if (!workloadOp.isPresent())
//            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
//        NepuResearchWorkload workload = workloadOp.get();
//        return getParticipantByParams(workload.getResearchType(),workload.getMapperId());
//    }

//    @GetMapping("workload/part")
    @ApiOperation(value = "查询参与人员(params)",notes = "根据传入的科研类型和ID查询")
    public ResponseDTO getParticipantByParams(
            @RequestParam("researchType") String researchType,
            @RequestParam("mapperId") String mapperId
    ){
        List<Long> userIds = new ArrayList<>();
        if (researchType.equals(ResearchTypeConst.PAPER)){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM,"论文不需要分配工作量");
        }
        else if (researchType.equals(ResearchTypeConst.AWARD)){
            Optional<Object> awardOp = researchServiceCtl.getById(mapperId,ResearchTypeConst.AWARD);
            if (!awardOp.isPresent())
                return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
            Optional<Object> nepuAwardOp =  researchServiceCtl.getById(mapperId,ResearchTypeConst.AWARD);
            NepuAward award = new NepuAward();
            BeanUtil.copyProperties(nepuAwardOp.get(),award, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            userIds = award.getUserIdList();
        }
        else if (researchType.equals(ResearchTypeConst.THINKTANK)){
            Optional<Object> thinkTankOp = researchServiceCtl.getById(mapperId,ResearchTypeConst.THINKTANK);
            if (!thinkTankOp.isPresent())
                return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
            Optional<Object> nepuThinkTankOp =  researchServiceCtl.getById(mapperId,ResearchTypeConst.THINKTANK);
            NepuThinkTank thinkTank = new NepuThinkTank();
            BeanUtil.copyProperties(nepuThinkTankOp.get(),thinkTank, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            userIds = thinkTank.getUserIdList();
        }
        else if (researchType.equals(ResearchTypeConst.TOPIC)){
            Optional<Object> topicOp = researchServiceCtl.getById(mapperId,ResearchTypeConst.TOPIC);
            if (!topicOp.isPresent())
                return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
            Optional<Object> nepuTopicOp =  researchServiceCtl.getById(mapperId,ResearchTypeConst.TOPIC);
            NepuTopic topic = new NepuTopic();
            BeanUtil.copyProperties(nepuTopicOp.get(),topic, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            userIds = topic.getUserIdList();
        }
        else if (researchType.equals(ResearchTypeConst.BOOK)){
            Optional<Object> bookOp = researchServiceCtl.getById(mapperId,ResearchTypeConst.BOOK);
            if (!bookOp.isPresent())
                return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
            Optional<Object> nepuBookOp =  researchServiceCtl.getById(mapperId,ResearchTypeConst.BOOK);
            NepuBook book = new NepuBook();
            BeanUtil.copyProperties(nepuBookOp.get(),book, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            userIds = book.getUserIdList();
        }
        else if (researchType.equals(ResearchTypeConst.PROPERTY)){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM,"知识产权不需要分配工作量");
        }
        else if (researchType.equals(ResearchTypeConst.SUBJECT)){
            Optional<Object> subjectOp = researchServiceCtl.getById(mapperId,ResearchTypeConst.SUBJECT);
            if (!subjectOp.isPresent())
                return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
            Optional<Object> nepuSubjectOp =  researchServiceCtl.getById(mapperId,ResearchTypeConst.SUBJECT);
            NepuSubject subject = new NepuSubject();
            BeanUtil.copyProperties(nepuSubjectOp.get(),subject, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            userIds = subject.getUserIdList();
        }
        List<SysUser> users = userService.findByIds(userIds);
        List<SysUserBasic> userBasics = new ArrayList<>();
        users.forEach(user -> {
            SysUserBasic basic = userService.buildBasic(user);
            userBasics.add(basic);
        });
        return ResponseDTO.succData(userBasics);
    }

}
